package com.pig4cloud.pigx.wallet.configuration;


import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pig4cloud.pigx.wallet.constant.WalletConstant;
import com.pig4cloud.pigx.wallet.entity.Currency;
import com.pig4cloud.pigx.wallet.service.CurrencyService;
import lombok.RequiredArgsConstructor;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

@Configuration
@RequiredArgsConstructor
public class Web3jConfiguration {

	private final CurrencyService currencyService;

	@Bean
	public Web3j web3j(){
		Currency currency = currencyService.getOne(new LambdaQueryWrapper<Currency>()
				.eq(Currency::getPublicChain, WalletConstant.ETH)
				.eq(Currency::getState, WalletConstant.TRUE)
				.eq(Currency::getName, WalletConstant.USDT)
		);
		return Web3j.build(new HttpService(currency.getNodeAddress()+currency.getApiKey()));
	}

	@Bean
	public AES aes(){
		return new AES(Mode.CFB, Padding.NoPadding,
				new SecretKeySpec("pigxpigxpigxpigx".getBytes(), "AES"),
				new IvParameterSpec("pigxpigxpigxpigx".getBytes()));
	}

}
